#include "MLTestTools.h"
#include "../Tools/ZLogManager.h"
#include <fstream>

GLOBAL_NAMESPACE_BEGIN

namespace MachineLearningTest
{
    bool MLTestTools::saveDataAsObjFile(const char* fileName, const Eigen::MatrixXd& data)
    {
        std::ofstream fout(fileName);
        if (!fout)
            return false;

        int dataSize = data.rows();
        int dataDim  = data.cols();
        if (dataDim!=2 && dataDim!=3)
        {
            DebugLog << "data with dimension: " << dataSize << " * " << dataDim;
            DebugLog << "Not support data with dimension different with 2 and 3";
            return false;
        }
        for (int i = 0; i < dataSize; i++)
        {
            fout << "v ";
            for (int j = 0; j < dataDim; j++)
            {
                fout << data(i, j) << " ";
            }
            if (dataDim==2)
                fout << "0";
            fout << "\n";
        }

        fout.close();
        return true;
    }

    bool MLTestTools::saveDataToFile(const char* fileName, const Eigen::MatrixXi& data)
    {
        std::ofstream fout(fileName);
        if (!fout)
            return false;

        int dataSize = data.rows();
        int dataDim  = data.cols();
        fout << dataSize << " " << dataDim << "\n";
        for (int i = 0; i < dataSize; i++)
        {
            for (int d = 0; d < dataDim; d++)
            {
                fout << data(i, d) << " ";
            }
            fout << "\n";
        }

        fout.close();
        return true;
    }

    bool MLTestTools::saveDataToFile(const char* fileName, const Eigen::MatrixXd& data)
    {
        std::ofstream fout(fileName);
        if (!fout)
            return false;

        int dataSize = data.rows();
        int dataDim = data.cols();
        fout << dataSize << " " << dataDim << "\n";
        for (int i = 0; i < dataSize; i++)
        {
            for (int d = 0; d < dataDim; d++)
            {
                fout << data(i, d) << " ";
            }
            fout << "\n";
        }

        fout.close();
        return true;
    }

    bool MLTestTools::loadDataFromFile(const char* fileName, Eigen::MatrixXd& data)
    {
        std::ifstream fin(fileName);
        if (!fin)
            return false;

        int nSize=-1, nDim = -1;
        double x=0, y=0;
        fin >> nSize >> nDim;
        if (nSize<=0 || nDim<=0)
        {
            fin.close();
            DebugLog << "File not correct";
            return false;
        }
        data.resize(nSize, nDim);
        for (int i = 0; i < nSize; i++)
        {
            for (int j = 0; j < nDim; j++)
            {
                fin >> data(i, j);
            }
        }
        fin.close();
        return true;
    }

    bool MLTestTools::loadDataFromFile(const char* fileName, Eigen::MatrixXi& data)
    {
        std::ifstream fin(fileName);
        if (!fin)
            return false;

        int nSize=-1, nDim = -1;
        double x=0, y=0;
        fin >> nSize >> nDim;
        if (nSize<=0 || nDim<=0)
        {
            fin.close();
            DebugLog << "File not correct";
            return false;
        }
        data.resize(nSize, nDim);
        for (int i = 0; i < nSize; i++)
        {
            for (int j = 0; j < nDim; j++)
            {
                fin >> data(i, j);
            }
        }
        fin.close();
        return true;
    }
}

GLOBAL_NAMESPACE_END
